<?php
class Controller {
 	protected $tmpl_path;
 	protected $user	= null;
 	protected $req 	= Array();
 	public $Content	= null;
 	public $Error 	= null;
 	public $ajax 	= Array();
 	protected $cache= Array(); 
	
	function __construct($controller_patch){				
		$this->user 		= Core::$user;
		$this->req  		= $_REQUEST;
		$this->tmpl_path 	= $controller_patch.DIRECTORY_SEPARATOR."templates".DIRECTORY_SEPARATOR;
		$this->tmpl_path_origin	= $controller_patch.DIRECTORY_SEPARATOR."templates".Origin::get_sufix().DIRECTORY_SEPARATOR;
		$this->cache['CACHE_DIRECTORY']     = CFG_PATH_HTDOCS. DIRECTORY_SEPARATOR . "tmpl_cache". DIRECTORY_SEPARATOR . get_class($this).DIRECTORY_SEPARATOR;
        $this->cache['CACHE_LIFETIME']     	= 157680000;
		
	}
 	
 	public function setControlParam($param = Array()){
 		if(!empty($param)){
 			$this->req = $param;
 		}
 	}
 	
 	private function getControllerName(){
 		$tmp = get_class($this);
 		$tmp = explode("_", $tmp);
 		return $tmp[0];
 	}
 	// Контроллеоры к которым имеем доступ во время боя
 	private $cAccess = 
 		Array(
 		// Только Боевка
 			"Game",
 			"Combat",
 			"Chat",
 		);
 	private function access(){
 	// Модуль не требует авторизации
 		$name = $this->getControllerName(); 	 	
 		
 		if( !CoreConfig::getController( $name )->isAuth ){
 			return true;
 		}elseif(CoreConfig::getController( $name )->isAuth && ($this->user instanceof ava_users) ){
 			/*if($this->user->battle_id > 0 &&  array_search($name, $this->cAccess) === false){
 				header("Location: /Combat/");
 			}*/
 			return true;
 		}
 		return false;
 	}
 	
 	public function getMethod($method_name){
 		if( $this->access() ){
 			if(method_exists($this, $method_name)){
				ob_start();
				try{
 					$this->$method_name();
 					$this->Content = ob_get_contents();
 					ob_end_clean();
				}catch(ModelException $e){
					$this->$method_name();
 					$this->Content = ob_get_contents();
 					ob_end_clean();				
				}
 			}else{
 				$this->Error( "Не верный вызов!" );
 			}
 		}else{
 			//var_dump($_SESSION);
 			//var_dump($_COOKIE);
			//$this->Error( "Вы не авторизированы. Возможно вы используете браузер Safari." );
			
			if($this->req["ajax"]){
				$this->ajax[] = Array("a"=>"user.auth", "d"=>"logout");
			}else{
				$this->Content = "<script> 
					var current = window.self; 
					if(current == window.top){
						//alert('Окно родитель');
						window.location.href='/';
					}else{
						//alert('Окно дочернее');
						window.top.location.href='/';
					}
					</script>
				";
			}
		}
		//$this->ajax[] = Array("a"=>"user.auth", "d"=>"logout");
 	}
 	
 	public function Error( $message ){
 		$this->Error = $message;
 	}
 	
 	// Вывести сообщение
	public function message($message, $title, $type)
	{
		$this->ajax[] = Array(
			"a" => "infoMessage",
			"d" => Array("title" 	=> $title,
						 "content" 	=> $message, 
                         "type"     => $type)
		);
	}
 	
}
?>